package com.android.linebirds.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.android.linebirds.LineBirdsGame;
import com.android.linebirds.LineBirdsGame.OnGameListener;
import com.android.linebirds.R;
import com.android.physics.Config;

public class PlayActivity extends Activity
{
    private LineBirdsGame mLineBirdsGame;
    private TextView mCurrentScore;
    private Button mPauseButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        initConfig();
        initWidget();
    }
    
    @Override
    protected void onDestroy()
    {
        unInitConfig();
        super.onDestroy();
    }
    
    private void initConfig()
    {
        Config.initSingleton();
        int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
        Config.instance().getStage().setSize(screenWidth, screenHeight);
        float gravity = screenHeight/1000f;
        Config.instance().getStage().setGravity(gravity);
        Config.instance().getStage().setMaxSpeed(7);
        Config.instance().getBird().setSize(screenHeight/10, screenHeight/10);
        Config.instance().getBird().setStep(0, -2 * gravity);
        Config.instance().getBird().setStartPos(screenHeight/10, screenHeight/2);
        
        Config.instance().setTimeInterval(20);
        int speed = screenWidth/100;
        Config.instance().getStage().setSpeed(speed);
        Config.instance().getRoadblock().setLifeCycle(screenWidth/speed);
        Config.instance().getRoadblock().setWidthRange(10, 12);
        Config.instance().getRoadblock().setHeightRange(
                screenHeight/4,
                (screenHeight - Config.instance().getBird().getHeight())/2);
        Config.instance().getRoadblock().setTimeStampIntervalRange(10, 25);
    }
    
    private void unInitConfig()
    {
        Config.uninitSingleton();
    }
    
    
    private void initWidget()
    {
        setContentView(R.layout.play);
        mLineBirdsGame = (LineBirdsGame) findViewById(R.id.line_bird_view);
        mLineBirdsGame.setOnGameListener(mOnGameListener);
        mLineBirdsGame.init();
        
        mCurrentScore = (TextView) findViewById(R.id.current_score);
        updateScore(0);
        
        mPauseButton = (Button) findViewById(R.id.btn_pause);
        mPauseButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mLineBirdsGame.pause();
            }
        });
    }
    
    @Override
    public void finish()
    {
        mLineBirdsGame.exit();
        super.finish();
    }
    
    private OnGameListener mOnGameListener = new OnGameListener()
    {
        @Override
        public void onScoreUpdate(long score)
        {
            updateScore(score);
        }
        
        @Override
        public void onGameOver()
        {
            
        }
    };
    
    private void updateScore(long score)
    {
        String string = Long.toString(score);
        int length = string.length();
        mCurrentScore.setText(getRepeatZero(MAX_LENGTH - length) + string);
    }
    

    private final int MAX_LENGTH = 10;
    private char[] CHARS;
    private String getRepeatZero(int count)
    {
        if (count < 0)
            return "";
        
        if (CHARS == null)
        {
            CHARS = new char[MAX_LENGTH];
            for (int i = 0; i < CHARS.length; i++)
            {
                CHARS[i] = '0';
            }    
        }
        return new String(CHARS, 0, count);
    }
}
